<template>
  <div>
    <el-dialog center :visible.sync="is_show" title="修改客户信息" @closed="closed">
      <div>
        <el-form :model="form" ref="edit-form" :rules="rules">
          <el-row>
            <el-col :span="11">
              <el-form-item label="姓名" prop="name">
                <!-- <el-autocomplete
                  v-model="form.name"
                  :fetch-suggestions="querySearch"
                  :trigger-on-focus="false"
                  placeholder="请输入姓名"
                  :validate-event="false"
                ></el-autocomplete> -->
                 <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="2">
              <el-form-item label="身份证号" prop="id_card">
                <el-input v-model="form.id_card" placeholder="请输入身份证号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="2">
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="所属单位" prop="organization">
                <el-input v-model="form.organization" placeholder="请输入所属单位"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="11" :offset="2">
              <el-form-item label="注册时间" prop="created_at">
                <el-input v-model="form.created_at" placeholder="请输入注册时间"></el-input>
              </el-form-item>
            </el-col> -->
          </el-row>
        </el-form>
      </div>
      <template slot="footer">
        <el-button @click="is_show = false" v-no-more-click:3000>取消</el-button>
        <el-button type="primary" @click="submit" v-no-more-click:3000>确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  validateNull,
  validateOrganization,
  validateId,
  validatePhone,
  validateEmail
} from "~/util/index";
export default {
  data() {
    return {
      is_show: false,
      form: {
        name: "",
        email: "",
        id_card: "",
        phone: "",
        organization: "",
        // created_at: ""
      },
      user_id: null,
      rules: {
        name: [
          { required: false, validator: validateNull, trigger: "blur" }
        ],
        organization: [
          { required: false, validator: validateOrganization, trigger: "blur" }
        ],
        id_card: [{ required: false, validator: validateId, trigger: "blur" }],
        phone: [
          { required: false, validator: validatePhone, trigger: "blur" }
        ],
        email: [
          { required: false, validator: validateEmail, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    show(data) {
      for(let i in this.form) {
          this.form[i] = data[i]
      }
      this.user_id = data.id
      this.is_show = true;

    },
    submit() {
      this.$refs["edit-form"].validate(valid => {
        if (valid) {
          this.submitPost();
        } else {
          this.$message({
            type: "error",
            message: "校验失败，请检查填写的资料"
          });
          return false;
        }
      });
    },
    submitPost() {
      if (this.user_id) {
        this.$axios.put(`/api/liaison/${this.user_id}`, this.form).then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "用户信息修改成功"
            });
            this.$emit("changeList");
            this.is_show = false;
          }
        });
      } else {
        this.$message({
          type: "error",
          message: "该用户信息不允许修改。"
        });
      }
    },
    closed() {},
  }
};
</script>

<style lang="less" scoped>
/deep/.el-dialog {
  .el-dialog__body {
    padding-bottom: 0;
    .el-cascader,
    .el-select,
    .el-autocomplete {
      width: 100%;
    }
  }
  .el-dialog__footer {
    text-align: center;
  }
}
</style>
